using System;
using System.Threading;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using XYThreadPoolLib;
using System.Diagnostics;

namespace CommLayer
{
    class InputProcessor
    {
        protected delegate void ProcessInputDelegate(Transmission trn, IPEndPoint ipe);

        protected const int m_nPause = 25;
        protected int m_nReadTimeout = 30;
        protected int m_nMaxDataSize = 4 * 1024 * 1024;
        protected XYThreadPool m_threadPool = null;
        public BinaryInputHandlerDelegate m_delegateBinaryInputHandler = null;
        public StringInputHandlerDelegate m_delegateStringInputHandler = null;
        public Exception m_exception = null;

        /// <summary>
        /// Generic, 4-bytes header input processor
        /// </summary>
        /// <param name="delegateConnectionFilter"></param>
        /// <param name="delegateStringInputHandler"></param>
        /// <param name="delegateExceptionHandler"></param>
        public InputProcessor(StringInputHandlerDelegate delegateStringInputHandler,XYThreadPool threadPool)
        {
            m_delegateStringInputHandler = delegateStringInputHandler;
            ThreadPool = threadPool;
         }

        /// <summary>
        /// destructor
        /// </summary>
        ~InputProcessor()
        {
            Close();
        }

        /// <summary>
        /// Setter for the thread pool
        /// </summary>
        public XYThreadPool ThreadPool
        {
            set
            {
                m_threadPool = value;
            }
        }


        /// <summary>
        /// starts the main thread
        /// </summary>
        /// <param name="trn"></param>
        /// <param name="ipe"></param>
        public virtual void Start(Transmission trn, IPEndPoint ipe)
        {
            // start the input processin thread 
            m_threadPool.InsertWorkItem("Process Input", new ProcessInputDelegate(ProcessInput), new Object[2] { trn, ipe }, false);
        }


        /// <summary>
        /// 4-bytes header receive data input process 
        /// </summary>
        /// <returns></returns>
        public virtual void ProcessInput(Transmission trn, IPEndPoint ipe)
        {
            // cannot be called
              Debug.Assert(false);
        }

        /// <summary>
        /// Overriable to allow special processing upon end of input process
        /// </summary>
        public virtual void EndInputProcessor(Transmission trn)
        {
            // server would remove transmission

        }


        /// <summary>
        /// Closes input processor threads
        /// </summary>
        public virtual void Close()
        {
            
        }

    }
}
